package com.android.base.util;

import android.util.Log;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 签名工具
 * Created by Administrator on 2016/7/22.
 */
public class JfwSignHelper {

    /**
     *网络请求签名
     */
    public static Map<String,String> doBizParams(Map<String,String> params){
        //常用参数
        if(params == null){
            params = new HashMap<String,String>();
        }
        //token 不为空时添加，uid等
//        if(EHAApplication.sp.getString("token",null) != null){
//            params.put("token", EHAApplication.sp.getString("token",null));
//            if(params.get("uid") == null){
//                params.put("uid",EHAApplication.sp.getInt("uid",-1) + "");
//            }else if(params.get("uid").toString().equals("-1")){
//                params.remove("uid");
//            }else{
//
//            }
//        }

        params.put("client_key","aec39AnV");
        params.put("request_time", System.currentTimeMillis() + "");
        params.put("device_token","KKASHDKAHSDKAKHS");

        String str = getSortRequestData(params);
        String mixString = mixString(md5(str), new StringBuilder("1n9t9kYWhCKQQuKs").reverse().toString());
        String sign = md5(mixString.substring(0,32));
        params.put("sign",sign);
        for(Map.Entry<String, String> entry: params.entrySet()) {
            Log.d("yqy",entry.getKey() + ":" + entry.getValue() + "\t");
        }
        return params;
    }

    /**
     * 对请求的数据按key排序
     * @param requestData
     * @return
     */
    private static String getSortRequestData(Map<String,String> requestData){
        List<String> keys=new ArrayList<String>(requestData.keySet());
        Collections.sort(keys);
        StringBuilder requestDataStringBuilder = new StringBuilder();
        for(String key:keys){
            requestDataStringBuilder.append(key).append(requestData.get(key));
        }
        return requestDataStringBuilder.toString();
    }

    /**
     * md5加密，生成32位的加密后的字符串
     * @param input
     *            输入参数
     * @return
     */
    public static String md5(String input) {
        String ret = null;
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f' };
        try {
            byte[] btInput = input.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest(); // MD5 的计算结果是一个 128 位的长整数，共16个字节
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            ret = new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 梅花间竹混合字符串
     * @param str1
     * @param str2
     * @return
     */
    public static String mixString(String str1, String str2) {
        int len1 = str1.length();
        int len2 = str2.length();
        int minLen = len1 > len2 ? len2 : len1;
        String maxStr = len1 > len2 ? str1 : str2;
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (index < minLen) {
            sb.append(str1.charAt(index)).append(str2.charAt(index));
            index++;
        }
        sb.append(maxStr.substring(index));
        return sb.toString();
    }
}
